import requests
from lxml import etree
import DBUtil
import Sha1Util
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                  'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'
}
def quyu():
    sql = "select url,name,id from `guangdong` "
    result = DBUtil.selectTable(sql)
    for i in result:
        url = i[0]
        naem = i[1]
        wangzhan3 = requests.get(url=url, headers=headers)
        cun3 = wangzhan3.text
        tree = etree.HTML(cun3)
        quyu_url=tree.xpath('/html/body/div[3]/div/div[1]/dl[2]/dd/div[1]/div/a/@href')
        quyu_name=tree.xpath('/html/body/div[3]/div/div[1]/dl[2]/dd/div[1]/div/a/text()')
        new_quyu_url=[]

        for i in quyu_url:
            new_quyu_url.append('https://' + url.split('/')[2]+i)
        region_name_href_dict = dict(zip(quyu_name, new_quyu_url))

        id = Sha1Util.jia_mi(naem + url)
        for i, l in region_name_href_dict.items():
            sql = '''INSERT into `quyu` (chengshi_id,name,url) VALUES ('%s','%s','%s')
                                                ''' % (id, i, l)
            DBUtil.updateTable(sql)
quyu()
